package Methods;

import SeqList.SeqList;
import TripleTable.TripleTable;

public class SearchShortest {
    TripleTable data;
    int[][] compromise;//实在没有办法了……
    int count;//记录二维数组中的行数

    public Object[] searchShortest(int id1, int id2) {//查询两景点间最短路径
        SeqList path = new SeqList(this.data.length());
        int shortestPath = 0;
        int leastWeight = -1;
        SearchAll allPath = new SearchAll();
        this.compromise = allPath.searchAll(id1, id2);
        for (int i = 0; i < count; i++) {//循环比较每一条路径的权重大小
            int j = 0;
            int totalWeight = 0;
            while(this.compromise[i][j] != 0){//求本路径的权重
                totalWeight += this.data.getWeight(this.compromise[i][j]);
                j++;
            }
            if (leastWeight == -1 || leastWeight > totalWeight){//比较现最短路径权重和本路径权重大小
                shortestPath = i;
                leastWeight = totalWeight;
            }
        }
        int k = 0;
        while (this.compromise[shortestPath][k] != 0){//将最短路径赋给path
            path.set( k, this.compromise[shortestPath][k]);
            k++;
        }
        return path.getElements();
    }
}
